Data Visualization Using R in Undergraduate Macroeconomics

James M. Murray, Ph.D.

Department of Economics
University of Wisconsin-La Crosse
Email: jmurray@uwlax.edu


St. Louis Fed Professors’ Conference

November 8, 2024

QR Code

Data Visualization Using R in Undergraduate Macroeconomics

Want to start using the ECODATA R package right now?

Live RStudio project on Posit Cloud!

  1. Login / Create Posit Cloud account at https://posit.cloud

  2. Create your own copy of the ECODATA Exercises project by following:

    https://posit.cloud/content/9084731

  3. Login / Create FRED account at https://fred.stlouisfed.org/
    (click My Account at top left)

  4. Get your FRED key at https://fredaccount.stlouisfed.org/apikeys

  5. Copy that 32-character key and in the Posit Cloud console, set the key:
    ecodata::ecodata_set_fredkey("abcd1234efgh5678ijkl9012mnop3456")

Learn more at https://murraylax.org/ecodata/

QR Code

Great Recession & Slow Recovery

QR Code

The Good, The Bad, and the Ugly

QR Code

The Good:

  • Student explored data on both FRED and World Bank Data

  • Student picked relevant variables, explained it correctly

  • Cogent international comparisons of the great recession

The Bad:

  • Great interactive tools made for poor visualizations (at least default values)

  • Time consuming to reproduce - Return to website, download image, etc.

The Ugly:

  • Inconsistent style across FRED and World Bank Data

  • Very difficult to see much of the text

QR Code

Introduction

  • Created an R package, ecodata, to facilitate visualizing data in undergraduate macroeconomics courses

  • Wrapper for the fredr, wbstats, and ggplot2 packages

  • Makes it easy to:

    • Download data from the Federal Reserve Economic Data (FRED) and World Bank databases

    • Produce professional-quality data visualizations

    • View descriptions of the data

    • Cite sources

  • Do everything with 3-4 lines of code

  • Not a substitute for fredr, wbstats, or ggplot2, nor for exploring data and using interactive tools in FRED and World Bank Data

Let’s Try It Out!

QR Code

Why Scripting Languages?

  • Reproducible: The code is the set of instructions for what you created

  • Efficient: Difficult to make the first graph, but it’s easy to make the 2nd and 100th graphs!

  • Flexible: Can easily change the data, the graph, the labels, etc.

  • Used in other courses: R is used in econometrics, statistics, data science, etc.

  • Used in industry, even among those who are not data scientists

  • Even more important / relevant with AI

    • AI assistance makes coding more accessible

    • Verification and reproducibility is key with AI-generated content

QR Code

Why Avoid Scripting Languages?

  • Steep learning curve

  • Easy to do the hard things, but hard to do the easy things

  • This is Principles of Macroeconomics, not what I signed up for!

ECODATA Package

  • Gentle learning curve!

  • Easy to do easy things

  • Use simple reproducible code to analyze macroeconomics principles

QR Code

Why ECODATA for Principles of Macroeconomics?

  • Data visualization is a key skill for everyone

  • Early introduction makes subsequent classes easier

  • Data is used throughout the economics discipline, it should be used throughout economics curriculum

  • Repeated practice throughout undergraduate degree builds mastery

QR Code

Best Practices for Principles-Level

  • Show students worked-out examples - See my vignettes!

  • Create prompts that require only 1-2 lines of code to answer the question

  • Make economics real: Compare theoretical predictions with recent data

  • Be mindful of opportunity costs - Do more data analysis means doing less of something else in the class

    • I plan on 3 or 4 short assignments for the semester

QR Code

Example

  1. Download data for the U.S. unemployment rate, California unemployment rate, and Wisconsin unemployment rate from FRED, and filter the data to only include the years 2006-2016.

  2. Plot the U.S. unemployment rate from 2006-2016. Describe the behavior of the unemployment rate before, during, and years after the recession. Is the situation back to normal at the end of the recession? What do you notice about economic recoveries?

  3. Plot the U.S. unemployment rate along with Wisconin’s and California’s unemployment rate. What do you notice about the differences in the unemployment rates between these states?

QR Code

Example - #1: Download Data

data_sources <- c(
  "https://fred.stlouisfed.org/series/UNRATE",
  "https://fred.stlouisfed.org/series/WIUR",
  "https://fred.stlouisfed.org/series/CAUR"
)

variable_names <- c("U.S. Unemployment Rate", "Wisconsin Unemployment Rate", "California Unemployment Rate")

# Download the data
udata <- get_ecodata(data_sources, variable_names)

# Filter for only 2006-2013
udata <- udata |>
  filter(Date >= "2006-01-01", Date <= "2018-12-31")

QR Code

Example - #2: U.S. Unemployment Rate

ggplot_ecodata_ts(udata, 
                  variables = "U.S. Unemployment Rate",
                  title = "U.S. Unemployment Rate",
                  plot.recessions = TRUE)

QR Code

Example - #3: Plot U.S., Wisconsin, and California Unemployment Rates

ggplot_ecodata_ts(udata, title = "Unemployment Rates", plot.recessions = TRUE)

QR Code

Instructional Vignettes

QR Code

What Are Your Ideas?

  • You have or envision a short low-stakes assignment where students visualize macroeconomic data?

  • Answer these questions about one such assignment:

    1. What is the macroeconomics concept or theory that students apply?

    2. What variables do the students need?

    3. What will the students discover?

  • Share your ideas! Email: jmurray@uwlax.edu

  • What me to create instructional vignette around your idea? Let me know!

Thank you!

QR Code